<?php
require_once('./includes/headers.inc.php');
require_once('./top.php');
require_once('./lib/getordinal.function.php');

$error = FALSE;

if(!isset($_GET['id'])) {
    $_SESSION['msg'] = $msgMgr->get('checkin-invalid-error');
    header('Location: /');
    exit();
}
$checkinId = (int)$_GET['id'];
$checkinData = $checkinMgr->get($checkinId);

//stop user from viewing other's checkins on this screen
if($checkinData['user_id'] != $_SESSION['user']['id']) {
    $_SESSION['msg'] = $msgMgr->get('access-denied-error');
    header('Location: /');
    exit();
}

$routeData = $routeMgr->get($checkinData['route_id']);
$location_count = $checkinMgr->getLocationCount($routeData['location_id'], $_SESSION['user']['id']);
$route_count = $checkinMgr->getRouteCount($checkinData['route_id'], $_SESSION['user']['id']);
$route_last_checkin = FALSE;
$route_last_comment = FALSE;
if($route_count > 1) {
    $last_checkin = $checkinMgr->getPreviousCheckin($checkinData['route_id'], $_SESSION['user']['id']);

    //include last comment, if any
    if($last_checkin['comment']) {
        $route_last_comment = '<ul><li>' . $langMgr->get('text_checkedin_last_comment', FALSE, array('comment' => '<i>' . $last_checkin['comment'] . '</i>')) . '</li></ul>';
        ;
    }
    //if last time you didn't make it and now you did, show that
    if($last_checkin['completed'] == -1 && $checkinData['completed'] >= 0) {
        $route_last_checkin = '<ul><li>' . $langMgr->get('text_checkedin_last_notcompleted') . '</li></ul>';
    //if last time you used a block and this time you didn't, show that
    } elseif($last_checkin['completed'] > 0 && $checkinData['completed'] == 0) {
        $route_last_checkin = '<ul><li>' . $langMgr->get('text_checkedin_last_block') . '</li></ul>';
    //if last time you used more blocks than this time, show that
    } elseif($checkinData['completed'] > 0 && $last_checkin['completed'] > $checkinData['completed']) {
        $route_last_checkin = '<ul><li>' . $langMgr->get('text_checkedin_last_moreblocks', FALSE, array('blockcount' => $last_checkin['completed'])) . '</li></ul>';
    //if last time you made it and this time you didn't, show that
    } elseif($last_checkin['completed'] >= 0 && $checkinData['completed'] == -1) {
        $route_last_checkin = '<ul><li>' . $langMgr->get('text_checkedin_last_completed') . '</li></ul>';
    }
}

$location_count = getOrdinal($location_count);
$route_count = getOrdinal($route_count);


switch($checkinData['completed']) {
    case -1: //didn't make it
        $completed_info = $langMgr->get('text_checkedin_notcompleted');
        break;
    case 0: //made it
        $completed_info = $langMgr->get('text_checkedin_completed');
        break;
    default: //made it, used block
        $completed_info = $langMgr->get('text_checkedin_blockcompleted', FALSE, array('blockcount' => $checkinData['completed']));
        break;
}

$replaces = array(
    'user_id'           => $_SESSION['user']['id'],
    'completed_info'    => $completed_info,
    'comment'           => ($checkinData['comment'] ? '<li>You said: <i>' . $checkinData['comment'] . '</i></li>' : ''),
    'route_count'       => $route_count,
    'route_lcomment'    => $route_last_comment,
    'route_lcheckin'    => $route_last_checkin,
    'error'             => $msgMgr->formatMsg($error),
);
$lang = array(
    'header_checkedin'              => $langMgr->get('header_checkedin'),
    'text_checkin_location_count'   => $langMgr->get('text_checkin_location_count', FALSE, array(
        'location_count' => $location_count,
        'location_id' => $routeData['location_id'],
        'location' => $routeData['location']
    )),
    'text_checkin_route_count'      => $langMgr->get('text_checkin_route_count', FALSE, array(
        'route_count'   => $route_count,
        'colour'        => $routeData['colour'],
        'difficulty'    => $routeData['difficulty'],
        'is_short'      => ($routeData['is_short'] ? ' ' . $langMgr->get('text_short') : ''),
        'route_id'      => $routeData['id'],
        'route_name'    => $routeData['name'],
    )),
    'link_back_home'                => $langMgr->get('link_back_home'),
    'link_my_profile'               => $langMgr->get('link_my_profile'),
    'link_checkin_again'            => $langMgr->get('link_checkin_again'),
);
$templateMgr->loadContent(basename(__FILE__, '.php'), array_merge($replaces, $lang));
?>
